package cn.edu.zafu.bigdata.yt.wlbc;

import java.io.*;
import java.net.*;

public class ChatServer_yt {
    public static void main(String[] args) {
        int port = 12345; 
        System.out.println("Chat Server is running...");

        try (ServerSocket serverSocket = new ServerSocket(port)) {
            System.out.println("Waiting for a client to connect...");

            Socket clientSocket = serverSocket.accept();
            System.out.println("Client connected: " + clientSocket.getInetAddress().getHostAddress());

            BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);

            String clientMessage;
            while ((clientMessage = in.readLine()) != null) {
                System.out.println("Client: " + clientMessage);
                if ("bye".equalsIgnoreCase(clientMessage)) {
                    System.out.println("Client disconnected.");
                    break;
                }
                out.println("Server received: " + clientMessage);
            }
        } catch (IOException e) {
            System.out.println("Server error: " + e.getMessage());
            e.printStackTrace();
        }
    }
}
